package com.twxm.breed.iot;

import com.twxm.breed.service.DissolvedOxygenService;
import com.twxm.breed.service.WaterFactorService;
import com.twxm.breed.service.WeatherFactorService;

import java.text.SimpleDateFormat;
import java.util.*;

public class IOTTask{

    private WaterFactorService waterFactorService;
    private WeatherFactorService weatherFactorService;
    private DissolvedOxygenService dissolvedOxygenService;

    public IOTTask(WaterFactorService waterFactorService) {
        this.waterFactorService = waterFactorService;
    }
    public IOTTask(WeatherFactorService weatherFactorService){
        this.weatherFactorService = weatherFactorService;
    }

    public IOTTask(DissolvedOxygenService dissolvedOxygenService){
        this.dissolvedOxygenService = dissolvedOxygenService;
    }


    public void getWaterFacterDataTask(String productKey, String deviceId, String pondId) throws Exception {
        String[] waterFactorStrs = {"nitrite",
                "permanganate",
                "totalPhosphorus",
                "totalNitrogen",
                "ammoniaNitrogen",
                "PH",
                "waterTemperature"};
        List<String> propertiesList = Arrays.asList(waterFactorStrs);

        HashMap<String,String> dataMap = ALiYunIOTAPI.getInstance().queryProperties(productKey,deviceId,propertiesList);
        dataMap.put("采集地点",pondId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        dataMap.put("采集时间",sdf.format(new Date()));

        List<HashMap<String,String>> list = new ArrayList<>();
        list.add(dataMap);
        list.forEach(map ->
        {
            System.out.println(map.get("nitrite"));
            System.out.println(map.get("permanganate"));
            System.out.println(map.get("totalPhosphorus"));
            System.out.println(map.get("totalNitrogen"));
            System.out.println(map.get("ammoniaNitrogen"));
            System.out.println(map.get("PH"));
            System.out.println(map.get("waterTemperature"));
        });
        waterFactorService.insertList(list);
    }


    public void getWeatherDataTask(String productKey, String deviceId, String pondId) throws Exception {
        String[] weatherFactorStrs = { "temperature",
        "windSpeed",
        "airPressure",
        "illumination",
        "humidity"};
        List<String> propertiesList = Arrays.asList(weatherFactorStrs);

        HashMap<String,String> dataMap = ALiYunIOTAPI.getInstance().queryProperties(productKey,deviceId,propertiesList);
        dataMap.put("采集地点",pondId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        dataMap.put("采集时间",sdf.format(new Date()));

        List<HashMap<String,String>> list = new ArrayList<>();
        list.add(dataMap);
        weatherFactorService.insertList(list);
    }

    public void getOxygenDataTask(String productKey, String deviceId, String pondId) throws Exception {
        String[] weatherFactorStrs = {"oxygen"};
        List<String> propertiesList = Arrays.asList(weatherFactorStrs);

        HashMap<String,String> dataMap = ALiYunIOTAPI.getInstance().queryProperties(productKey,deviceId,propertiesList);
        dataMap.put("采集地点",pondId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        dataMap.put("采集时间",sdf.format(new Date()));

        List<HashMap<String,String>> list = new ArrayList<>();
        list.add(dataMap);
        dissolvedOxygenService.insertList(list);
    }

    public void checkOxygenDataTask(String productKey, String deviceId, String pondId) throws Exception {
        String[] weatherFactorStrs = {"oxygen"};
        List<String> propertiesList = Arrays.asList(weatherFactorStrs);

        HashMap<String,String> dataMap = ALiYunIOTAPI.getInstance().queryProperties(productKey,deviceId,propertiesList);
        dataMap.put("采集地点",pondId);

    }
}


